package cn.jingzhuan.element.demo.view.epoxy.models

import android.support.v4.view.ViewPager
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.Toast
import cn.jingzhuan.element.demo.view.adapter.BasePagerAdapter
import cn.jingzhuan.element.demo.view.model.ItemDescription
import cn.jingzhuan.element.element.R
import cn.jingzhuan.element.element.databinding.ItemBannerBinding
import com.airbnb.epoxy.DataBindingEpoxyModel
import com.airbnb.epoxy.EpoxyModelClass

@EpoxyModelClass(layout = R.layout.item_banner)
abstract class BannerModel : DataBindingEpoxyModel() {


    var mBannerList: MutableList<ItemDescription> = mutableListOf()

    override fun buildView(parent: ViewGroup): View {
        val view = super.buildView(parent)
        var binding = view.tag as ItemBannerBinding
        binding.itemBannerPager.adapter = object : BasePagerAdapter<ItemDescription>(mBannerList) {
            override fun onBind(data: ItemDescription): View {
                val image = ImageView(parent.context)
                image.setImageResource(data.iconRes)
                image.scaleType = ImageView.ScaleType.CENTER_CROP
                image.setOnClickListener {
                    Toast.makeText(parent.context,"Click ${data.name}",Toast.LENGTH_SHORT).show()
                }
                return image
            }
        }
        binding.itemBannerIndicator.count = mBannerList.size
        binding.itemBannerPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
            override fun onPageScrollStateChanged(state: Int) {}

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {}

            override fun onPageSelected(position: Int) {
                binding.itemBannerIndicator.setSelected(position)
            }
        })
        return view
    }
}